package com.alliky.core.base

import android.content.Context
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import androidx.fragment.app.Fragment
import com.alibaba.android.arouter.launcher.ARouter
import com.alliky.core.R
import com.alliky.core.databinding.BaseFragmentLayoutBinding
import com.alliky.core.utils.RxToast
import com.alliky.core.widget.LoadingDialog

/**
 * ================================================
 * @Description: 描述
 * @Author: wxianing
 * @Date: 2022/11/9 15:16
 * ================================================
 */
abstract class BaseFragment<T : ViewDataBinding> : Fragment() {

    val TAG: String = this.javaClass.simpleName

    companion object {
    }

    private lateinit var mLoadingDialog: LoadingDialog

    lateinit var mBinding: T
    lateinit var mContext: Context
    lateinit var mBaseContainBinding: BaseFragmentLayoutBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        ARouter.getInstance().inject(this)
    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        mBaseContainBinding =
            DataBindingUtil.inflate(inflater, R.layout.base_fragment_layout, container, false)
        mBinding = DataBindingUtil.inflate(inflater, getLayoutId(), container, false)
        mBaseContainBinding.baseContainer.addView(mBinding!!.root)

        return mBaseContainBinding.root
    }

    override fun onAttach(context: Context) {
        super.onAttach(context)
        mContext = context
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        mLoadingDialog = LoadingDialog(view.context, false)
        initData(savedInstanceState)
        initObservable()
    }

    abstract fun getLayoutId(): Int
    abstract fun initData(savedInstanceState: Bundle?)
    abstract fun initObservable()

    fun showLoading() {
        mLoadingDialog.showDialog(mContext, false);
    }

    fun hideLoading() {
        mLoadingDialog.dismissDialog()
    }

    override fun onDestroy() {
        super.onDestroy()
        mBinding?.unbind()
    }

    fun showMessage(message: String) {
        RxToast.normal(message)
    }

    open fun isBackPressed(): Boolean {
        return false
    }
}